<template>
  <el-dialog append-to-body :before-close="cancel" :visible.sync="value">
    <template slot="title">成本总计</template>
    <el-table border :data="tableData" show-summary :summary-method="getSummaries">
      <el-table-column label="项目" prop="costName"></el-table-column>
      <el-table-column label="成本费用" prop="costTotal">
        <template slot-scope="{row}">{{(row.costName=='存款手续费'||row.costName=='提款手续费' ? row.costAmount:row.costTotal )|
          currency}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="比例(%)"><template slot-scope="{row}">{{row.costRate}}</template></el-table-column>
      <el-table-column label="结果" prop="costAmount">
        <template slot-scope="{row}">{{row.costAmount == 0 ? '无上限':row.costAmount}}</template>
      </el-table-column> -->
    </el-table>
    <div slot="footer" class="dialog-footer">

      <el-button type="primary" @click="$emit('input',false)">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {costCommission} from "@/services/api/commission";
  import {grid} from "@/build";

  export default {
    name: "apportion-commission-dialog",
    props: ["value", "id"],
    mixins: [grid],
    data() {
      return {
        statistic: [
          {
            index: 1,
            format: total => {
              return _.currency(total)
            }
          }
        ],
        tableData: []
      };
    },
    methods: {
      cancel() {
        this.$emit("input", false);
      },
      getSummaries(param) {
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '汇总';
            return;
          }
          const values = data.map(item => {
            if (item.costName.includes('存款手续费') || item.costName.includes('提款手续费')) return Number(item.costAmount)
            else return Number(item[column.property])
          });
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                let total = Number(prev) + Number(curr);
                return total;
              } else {
                return prev;
              }
            }, 0);
            sums[index] =  _.currency(sums[index]);
          } else {
            sums[index] = '';
          }
        });

        return sums;
      }
    },
    computed: {},
    mounted() {
      this.bindGet(costCommission, "tableData", {
        sendingData: {detailId: this.id},
      });
      this.getAll();
    }
  };
</script>

<style scoped>
</style>
